package future;

import org.junit.Test;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * Future测试
 *
 * @author codefish
 * @version 1.0
 * @date 2022/07/24 下午 02:13
 */
public class FutureTest {

    /**
     * 使用FutureTask实现一个异步任务
     */
    @Test
    public void testAsyncTask() throws InterruptedException, ExecutionException {
        FutureTask<String> futureTask = new FutureTask<>(new AsyncTask());
        Thread t1 = new Thread(futureTask, "异步线程");
        t1.start();

        Thread.sleep(1000);
        System.out.println("等待异步任务执行完成");
        System.out.println(futureTask.get());
    }


    /**
     * 阻塞等待策略来判断异步任务是否完成
     */
    @Test
    public void waitUntilTheTaskCompleted() throws InterruptedException, ExecutionException {

        FutureTask<String> futureTask = new FutureTask<>(new AsyncTask());
        Thread t1 = new Thread(futureTask, "异步线程");
        t1.start();

        //主线程执行一些任务
        System.out.println("主线程执行一些任务");
        try {
            TimeUnit.MILLISECONDS.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //调用get()方法时，等待异步任务执行完成,主线程被阻塞，无法执行后续任务
        try {
            System.out.println(futureTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        //主线程执行继续执行其它任务。。。
        System.out.println("主线程继续执行其它任务");
        try {
            TimeUnit.MILLISECONDS.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    /**
     * 轮询策略来判断异步任务是否完成
     */
    @Test
    public void pollUntilTheTaskCompleted() throws InterruptedException, ExecutionException {

        FutureTask<String> futureTask = new FutureTask<>(new AsyncTask());
        Thread t1 = new Thread(futureTask, "异步线程");
        t1.start();

        //主线程执行一些任务
        System.out.println("主线程执行一些任务");
        try {
            TimeUnit.MILLISECONDS.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //调用isDone()搭配重试机制对异步任务处理结果进行轮询，造成cpu空转
        while (true) {
            if (futureTask.isDone()) {
                System.out.println(futureTask.get());
                break;
            } else {
                System.out.println("异步任务没有完成，500ms后再询问");
                try {
                    TimeUnit.MILLISECONDS.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        //主线程执行继续执行其它任务。。。
        System.out.println("主线程继续执行其它任务");
        try {
            TimeUnit.MILLISECONDS.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    //异步任务对象
    class AsyncTask implements Callable<String> {

        @Override
        public String call() throws Exception {
            System.out.println("异步的线程：[" + Thread.currentThread().getName() + "]执行异步任务");
            Thread.sleep(5000);
            System.out.println("异步的线程：[" + Thread.currentThread().getName() + "]执行完成");
            return "hello world";
        }
    }


}
